using System;
using Microsoft.SharePoint;
using VariationSettings.Enums;

namespace VariationSettings
{
    /// <summary>
    /// The main program
    /// </summary>
    internal class Program
    {
        /// <summary>
        /// The main method of the program
        /// </summary>
        /// <param name="args">The arguments specified on the commandprompt</param>
        private static void Main(string[] args)
        {
            ProgramInputs inputs = new ProgramInputs(args);

            if (inputs.ArgsValid)
            {
                try
                {
                    using (SPSite site = new SPSite(inputs.Url))
                    {
                        using (SPWeb rootweb = site.RootWeb)
                        {
                            switch (inputs.CurrentOperation)
                            {
                                case Operation.Unknown:
                                    throw new InvalidOperationException("Invalid input operation specified or omitted");
                                case Operation.Import:
                                    WorkerBee.ImportSettings(inputs, rootweb);
                                    if (inputs.CreateHierarchy)
                                    {
                                        HttpBee.CreateHierachies(inputs);
                                    }
                                    break;
                                case Operation.Export:
                                    WorkerBee.ExportSettings(inputs, rootweb);
                                    break;
                                default: // just default
                                    break;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Unexpected error occurred:");
                    Console.WriteLine(ex.ToString());
                }
            }
            else
            {
                ProgramInputs.PrintHelp();
            }
        }
    }
}